package com.gaoding.shared.dict.controller;

import com.gaoding.framework.web.dto.PageInfo;
import com.gaoding.framework.web.util.WebUtils;
import com.gaoding.grpc.client.annotation.GdGrpcClient;
import com.gaoding.grpc.common.protobuf.Common;
import com.gaoding.grpc.common.utils.GdBeanUtils;
import com.gaoding.shared.dict.dto.request.CreateDictServiceLanguageDTO;
import com.gaoding.shared.dict.dto.request.QueryDictServiceLanguageDTO;
import com.gaoding.shared.dict.dto.request.UpdateDictServiceLanguageDTO;
import com.gaoding.shared.dict.dto.response.DictServiceLanguageRespDTO;
import com.gaoding.shared.dict.rpc.service.CreateDictServiceLanguageRequest;
import com.gaoding.shared.dict.rpc.service.DictServiceLanguageServiceGrpc;
import com.gaoding.shared.dict.rpc.service.QueryDictServiceLanguageRequest;
import com.gaoding.shared.dict.rpc.service.UpdateDictServiceLanguageRequest;
import com.google.protobuf.Int64Value;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mobai
 * @since 2021/4/15 14:03
 */
@RestController
@RequestMapping("/configs")
@Api(tags = "服务语言配置接口")
@CrossOrigin
public class DictServiceLanguageController extends BaseController<DictServiceLanguageRespDTO> {

    @GdGrpcClient
    private DictServiceLanguageServiceGrpc.DictServiceLanguageServiceBlockingStub client;

    /**
     * @param query
     * @return java.util.List<com.gaoding.shared.dict.dto.response.DictLanguageRespDTO>
     * @since 2021/2/19 11:44 上午
     */
    @GetMapping("list")
    @ApiOperation("分页查询")
    public List<DictServiceLanguageRespDTO> list(QueryDictServiceLanguageDTO query) {
        int pageNum = Integer.max(query.getPageNum() - 1, 0);
        Common.Page page = Common.Page.newBuilder().setPageSize(query.getPageSize()).setPageNum(pageNum).buildPartial();
        val queryParam = QueryDictServiceLanguageRequest.newBuilder().setServiceCode(query.getServiceCode() != null ? query.getServiceCode() : "").setPage(page).build();
        val result = client.list(queryParam)
                .getDataList().stream().map(data -> {
                    DictServiceLanguageRespDTO respDTO = new DictServiceLanguageRespDTO();
                    GdBeanUtils.copyProperties(data, respDTO);
                    return respDTO;
                }).collect(Collectors.toList());
        WebUtils.buildPageInfo(PageInfo.builder().num(query.getPageNum()).size(query.getPageSize()).build());
        return result;
    }

    @PostMapping("")
    @ApiOperation("新增单个服务配置")
    public Long create(@Valid @RequestBody CreateDictServiceLanguageDTO createDictServiceLanguageDTO) {
        CreateDictServiceLanguageRequest.Builder builder = CreateDictServiceLanguageRequest.newBuilder();
        GdBeanUtils.copyProperties(createDictServiceLanguageDTO, builder);
        Int64Value int64Value = client.create(builder.build());
        return int64Value.getValue();

    }

    @PutMapping("/{id}")
    @ApiOperation("更新单个服务配置")
    public void update(@PathVariable("id") Long id, @RequestBody @Valid UpdateDictServiceLanguageDTO updateDictServiceLanguageDTO) {
        updateDictServiceLanguageDTO.setId(id);
        UpdateDictServiceLanguageRequest.Builder builder = UpdateDictServiceLanguageRequest.newBuilder();
        GdBeanUtils.copyProperties(updateDictServiceLanguageDTO, builder);
        client.update(builder.build());

    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除单个服务配置")
    public void delete(@PathVariable("id") Long id) {
        client.delete(Int64Value.of(id));
    }

    @Override
    protected String[] args(DictServiceLanguageRespDTO dictServiceLanguage) {
        return new String[]{dictServiceLanguage.getServiceCode(),dictServiceLanguage.getLanguageCode(),dictServiceLanguage.getServiceCode(),dictServiceLanguage.getLanguageCode(),dictServiceLanguage.getVersion().toString()};
    }
}
